<?php
/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/12/20
 * Time: 17:01
 */

namespace App\Services;


use App\Models\Advice;
use App\Models\Contest;
use App\Models\Department;
use App\Models\IncreaseScoreApply;
use App\Models\Manager;
use App\Models\UserLevel;

class Service
{
    //党员收到的建议
    public static function getByUid($uid, $limit, $offset)
    {
        $query = Advice::where('is_del', 0)->where('user_id', $uid)->where('type', 1);
        $total = $query->count();
        $advices = $query->orderBy('id', 'desc')->limit($limit)->offset($offset)->get();
        $result = [];
        foreach ($advices as $advice) {
            $departmentName = '';
            if ($advice && $advice->department_id) {
                $department = Department::where('is_del', 0)->where('id', $advice->department_id)->first();
                $departmentName = $department->name;
            }
            $result[] = [
                'department_name' => $departmentName,
                'created_at' => date('Y-m-d H:i:s', strtotime($advice->created_at)),
                'content' => $advice->content
            ];
        }
        return [
            'total' => $total,
            'rows' => $result,
            'pages' => ceil($total / $limit),
            'page' => $offset / $limit + 1

        ];
    }

    //党支部收到的建议
    public static function getByDepartmentId($departmentId, $limit, $offset)
    {
        $query = Advice::where('is_del', 0)->where('department_id', $departmentId)->where('type', 2);
        $total = $query->count();
        $advices = $query->orderBy('id', 'desc')->limit($limit)->offset($offset)->get();
        $result = [];
        foreach ($advices as $advice) {
            $result[] = [
                'created_at' => date('Y-m-d H:i:s', strtotime($advice->created_at)),
                'content' => $advice->content
            ];
        }
        return [
            'total' => $total,
            'rows' => $result,
            'pages' => ceil($total / $limit),
            'page' => $offset / $limit + 1

        ];
    }

    public static function getContest($limit, $offset, $type=0)
    {
        $query = Contest::where('is_del', 0);
        if ($type) {
            $query = $query->where('type', $type);
        }
        $total = $query->count();
        $contests = $query->orderBy('id', 'desc')->limit($limit)->offset($offset)->get();
        $result = [];
        foreach ($contests as $contest) {
            $manager = Manager::where('id', $contest->manager_id)->first();
            $result[] = [
                'id' => $contest->id,
                'title' => $contest->title,
                'realname' => $contest->realname ? $contest->realname : ($manager ? $manager->realname : '')  ,
                'created_at' => date('Y-m-d H:i:s', strtotime($contest->created_at)),
            ];
        }
        return [
            'total' => $total,
            'rows' => $result,
            'pages' => ceil($total / $limit),
            'page' => $offset / $limit + 1

        ];
    }

    public static function getContestDetail($id)
    {
        $contest = Contest::where('is_del', 0)->where('id', $id)->first();
        if (!$contest) {
            return [];
        }

        return [
            'id' => $contest->id,
            'title' => $contest->title,
            'content' => $contest->content,
            'type' => $contest->type,
            'realname' => $contest->realname,
            'created_at' => date('Y-m-d H:i:s', strtotime($contest->created_at)),
        ];
    }

    public static function getIncreaseScoreDetail($id)
    {
        $increase = IncreaseScoreApply::where('is_del', 0)->where('id', $id)->first();
        if (!$increase) {
            return [];
        }

        return [
            'id' => $increase->id,
            'title' => $increase->title,
            'user_id' => $increase->user_id,
            'department_id' => $increase->department_id,
            'content' => $increase->content,
            'attachment' => $increase->attachment,
            'created_at' => date('Y-m-d H:i:s', strtotime($increase->created_at)),
            'score' => $increase->score
        ];
    }

    public static function getUserYearLevel($uid, $year)
    {
        $next = $year + 1;
        $level = UserLevel::where('user_id', $uid)->where('department_id', 0)->where('type', 3)->where('created_at', '>=', $year.'-01-01 00:00:00')->where('created_at', '<', $next.'-01-01 00:00:00')->first();
        return $level ? $level->level : 0;
    }
}